import com.mt.components.consts.PermissionName;
import com.mt.components.consts.SysConst;
import com.mt.components.general.Node;
import com.mt.components.log.LogFactory;
import com.mt.components.log.Logger;
import com.mt.components.utils.RemoteFactory;
import com.mt.components.utils.StringUtils;
import com.mt.system.domain.Role;
import com.mt.system.domain.User;
import com.mt.system.view.user.SaveUserView;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
import mx.utils.StringUtil;

/**      
 * 文件名称：SaveUserViewAS.as<br>
 * 文件描述：<br>
 * 创建人：马腾<br>
 * 创建时间：Nov 26, 2012 10:38:59 PM<br>
 * 修改人：马腾<br>
 * 修改时间：Nov 26, 2012 10:38:59 PM<br>
 */
private var logger:Logger = LogFactory.getLogger(SaveUserView);

[Bindable]private var roleNodes:ArrayCollection = new ArrayCollection();
[Bindable]public var user:User;

override protected function init(event:FlexEvent):void {
	super.init(event);
	
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("RoleService");
	remote.addEventListener(ResultEvent.RESULT , resultData);
	remote.getAllRoles();//查处所有的角色
}

protected function resultData(event:ResultEvent):void {
	var roles:ArrayCollection = event.result as ArrayCollection;
	for each(var role:Role in roles){//遍历角色封装tree的节点
		var node:Node = new Node();
		node.name = role.name;
		node.data = role;
		roleNodes.addItem(node);
	}
	if(user){//如果用户存在说明是修改用户，查处该用户的角色并勾选
		var remote:RemoteObject = RemoteFactory.getInstance().getRemote("RoleService");
		remote.addEventListener(ResultEvent.RESULT , function (e:ResultEvent):void {
			var nowRoles:ArrayCollection = e.result as ArrayCollection;
			for each(var r:Role in nowRoles){
				for each(var treeNode:Node in roleNodes){
					if(r.name == treeNode.data.name){
						treeNode.isSelected = true;
					}
				}
			}
		});
		remote.getRolesByUser(user);
	}
}

private function save():void {
	if(StringUtils.isEmpty(nameText.text)){
		mes.text = "用户名不能为空！";return;
	}
	if(StringUtils.isEmpty(passwordText.text) || StringUtils.isEmpty(confirmPwdText.text)){
		mes.text = "密码不能为空！";return;
	}
	if(StringUtil.trim(passwordText.text) != StringUtil.trim(confirmPwdText.text)){
		mes.text = "密码不一致！";return;
	}
	if(!StringUtils.isEmpty(phoneText.text) && !StringUtils.isPhone(phoneText.text)){
		mes.text = "电话格式有误！(13145678901)";return;
	}
	if(!StringUtils.isEmpty(emailText.text) && !StringUtils.isEmail(emailText.text)){
		mes.text = "邮件格式有误！(a@163.com)";return;
	}
	var selectedRoles:ArrayCollection = new ArrayCollection();
	for each(var node:Node in roleNodes){
		if(node.isSelected){
			selectedRoles.addItem(node.data as Role);
		}
	}
	if(selectedRoles.length == 0){
		mes.text = "请选择角色！";return;
	}
	var temp:User = new User();
	temp.name = StringUtil.trim(nameText.text);
	temp.password = StringUtil.trim(passwordText.text);
	temp.phone = StringUtil.trim(phoneText.text);
	temp.email = StringUtil.trim(emailText.text);
	temp.sex = sexButGroup.selectedValue as String;
	temp.roles = selectedRoles;
	temp.address = StringUtil.trim(addressText.text);
	setEnable(false);
	var remote:RemoteObject;
	if(user == null){
		remote = RemoteFactory.getInstance().getRemote("UserService" , PermissionName.USER_ADD);
		remote.addUser(temp);
	}else{
		temp.id = user.id;
		remote = RemoteFactory.getInstance().getRemote("UserService" , PermissionName.USER_MOD);
		remote.updateUser(temp);
	}
	remote.addEventListener(ResultEvent.RESULT , saveSuccess);
}

protected function saveSuccess(event:ResultEvent):void {
	super.close();
	this.dispatchEvent(new Event(SysConst.SAVE_SUCCESS));
}

protected function setEnable(isEnable:Boolean):void {
	saveButt.enabled = isEnable;
	cancleButt.enabled = isEnable;
}

